﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using RBS.Web.API.Models;
using JsonData = RBS.Common.JsonEntity.JsonData;

namespace RBS.Web.API.Areas.Order.Controllers
{
    /// <summary>
    /// 收到的邀请   
    /// </summary>
    public class DinnerController : ApiController
    {
        private ApiContext db = new ApiContext();

         
        /// <summary>
        /// 收到的邀请
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonData GetMyDinnerOrder(int uid, int page = 1, int limit = 5)
        { 
            //and id  in (select orderid from D_Guest where guestid = " + CurrentUser.Id + " 
            var jsonData = new JsonData { Success = true };
           //var items = db.Order.Where(c => c.OrderState == 2).Join(db.Guest,c=>c.Id,g=>g.OrderId, (c, g) => { c.Id});  
            var items =
                db.Order.SelectMany(c => db.Guest, (c, g) => new {c.Id,c.HotelName,c.OrderTime,c.ContactUser,g.Remark,g.Status,g.GuestId,g.OrderId,c.OrderState})
                  .Where(@t => @t.Id == @t.OrderId && @t.GuestId == uid)
                  .OrderBy(@t => @t.OrderTime);

            int total = items.Count();
            double pageCount = Math.Ceiling((double)total / (double)limit);
            int totalPage = (int)pageCount;
            if (page > totalPage)
            {
                page = totalPage;
                jsonData.Data = null;
                jsonData.Count = 0;
                jsonData.Page = page;
                jsonData.PageSize = limit;
                jsonData.TotalPage = totalPage;
                jsonData.Success = false;
                jsonData.Error = "最大页数为：" + totalPage + "页";
                return jsonData;
            }
            jsonData.Data = items.OrderByDescending(t => t.OrderTime).Skip((page - 1) * limit).Take(limit);
            jsonData.Count = total;
            jsonData.Page = page;
            jsonData.PageSize = limit;
            jsonData.TotalPage = totalPage;

            return jsonData;
        } 
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public JsonData PostGuest([FromBody]GuestModel value)
        {
            var jsonData = new JsonData { Success = true };
            var bll = new RBS.Core.BLL.D_Guest();
            jsonData.Success = bll.UpdateStatus(value.OrderId, value.UserId, value.Status, value.Remark);
            return jsonData;
        }
    }

    /// <summary>
    /// 食客接受饭局状态修改
    /// </summary>
    public class GuestModel
    {
        /// <summary>
        /// 订单ID
        /// </summary>
        public int OrderId { get; set; }
        /// <summary>
        /// 食客ID
        /// </summary>
        public int UserId { get; set; }
        /// <summary>
        /// 订单状态
        /// </summary>
        public int Status { get; set; }
        /// <summary>
        /// 订单备注
        /// </summary>
        public string  Remark { get; set; }

    }
}
